/**
 * Created by mengwei666 on 2017/1/2.
 */

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.commons.io.FileUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;

import java.io.*;


public class Test {
    static HttpConnect conn = new HttpConnect();
    static final int[] tid=new int[]{24,25,27,47,32,33,51,152,153,28,30,31,29,59,54,130,20,154,156,17,65,136,
            19,121,37,122,124,39,96,95,98,138,21,76,75,161,162,163,22,26,126,127,157,158,159,164,
    166,71,137,131};

    public static void main(String[] args) {
        try {
            DBOpener dbOpener=new DBOpener();
            long now = System.currentTimeMillis();
            for(int i=0;i<tid.length;i++) {
                for (int j = 1; j <= 400; j++) {
                    String url = String.format("http://api.bilibili.com/archive_rank/getarchiverankbypartion?callback=jQuery17202743760543707401_%d&type=jsonp&tid=%d&pn=%d&_=%d",
                            now, tid[i], j, now);
                    System.out.println(url);
                    HttpGet get = new HttpGet(url);
                    CloseableHttpResponse resp = conn.getClient().execute(get);
                    InputStream in = resp.getEntity().getContent();
                    String raw = readInput(in).substring(41);
                    raw=raw.substring(0,raw.length()-2);
                    resp.close();
                    JsonParser parser=new JsonParser();  //创建JSON解析器
                    JsonObject object=(JsonObject) parser.parse(raw);  //创建JsonObject对象
                    int isEmpty=object.get("code").getAsInt();
                    if(isEmpty==0){
                        JsonObject obj=object.get("data").getAsJsonObject();
                        JsonArray array = obj.get("archives").getAsJsonArray();    //得到为json的数组
                        for (int k = 0; k < array.size(); k++) {
                            System.out.println("---------------");
                            JsonObject subObject = array.get(k).getAsJsonObject();
                            String aid = subObject.get("aid").getAsString();
                            String author = subObject.get("author").getAsString();
                            String sort = subObject.get("tname").getAsString();
                            int sc = subObject.get("favorites").getAsInt();
                            dbOpener.insert(aid, author, sc, sort);
                        }
                        //Thread.sleep(500);
                    }
                }
            }
        }
        catch (Exception e) {

        }
    }

    private static String readInput(InputStream in) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buf = new byte[4096];
        int count = -1;
        while ((count = in.read(buf)) > 0) {
            out.write(buf, 0, count);
            out.flush();
        }
        out.close();
        String result = out.toString();
        return result;
    }
}
